package cn.wolfcode.web.controller;

import cn.wolfcode.qo.CustomerReportQueryObject;
import cn.wolfcode.service.ICustomerReportService;
import cn.wolfcode.service.impl.CustomerReportImpl;
import cn.wolfcode.util.MassageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {

    @Autowired
    private ICustomerReportService customerReportService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") CustomerReportQueryObject qo, Model model){
        PageInfo pageInfo = customerReportService.query(qo);
        model.addAttribute("pageResult",pageInfo);
        return "customerReport/list";
    }


    @RequestMapping("/bar")
    public String listBar(@ModelAttribute("qo") CustomerReportQueryObject qo, Model model){
        //统计报表信息,不需要分页
        List<HashMap> list = customerReportService.listAll(qo);
        //x轴数据
        ArrayList xList = new ArrayList<>();
        //y轴数据
        ArrayList yList = new ArrayList<>();
        //遍历list,获取每个map
        for (HashMap map : list) {
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }
        //因为freemarker页面不能直接使用${}获取集合对象,
        // 在共享到页面,需要转化为字符串,才可以使用${}
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList", JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MassageUtil.changMsg(qo));
        return "customerReport/bar";
    }


    @RequestMapping("/pie")
    public String listPie(@ModelAttribute("qo") CustomerReportQueryObject qo, Model model){
        //统计报表信息,不需要分页
        List<HashMap> list = customerReportService.listAll(qo);
        //legend数据
        ArrayList legendList = new ArrayList<>();
        //series数据
        ArrayList seriesList = new ArrayList<>();
        //遍历list,获取每个map
        for (HashMap map : list) {
            legendList.add(map.get("groupType"));
            //通过map获取数据,封装为新的map对象,里面的key需要有name和value的
            HashMap temp = new HashMap();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("number"));
            seriesList.add(temp);
        }
        model.addAttribute("legendList", JSON.toJSONString(legendList));
        model.addAttribute("seriesList", JSON.toJSONString(seriesList));
        model.addAttribute("groupTypeName", MassageUtil.changMsg(qo));
        return "customerReport/pie";
    }
}
